#include "state_group.h"
#include "state_info.h"


StateGroup::StateGroup(int64_t id, QString host, QString description, int port)
{
    state_info_ = new StateInfo();
    state_info_->setId(id);
    state_info_->setHost(host);
    state_info_->setStateName(description);
    state_info_->setDescription(description);
    state_info_->setPort(port);
    state_info_->setIsRtu(false);
}

StateGroup::~StateGroup()
{
    if(state_info_)
    {
        delete state_info_;
        state_info_ = nullptr;
    }
    for(auto state_info : state_infos_vec_)
    {
        delete state_info;
    }
    state_infos_vec_.clear();
}

int StateGroup::appendStateInfo(int64_t id, QString host, QString description, int port)
{
    // 检查是否存在相同的id
    for(auto state_info : state_infos_vec_)
    {
        if(state_info->id() == id)
        {
            return 0;
        }
    }
    StateInfo* state_info = new StateInfo();
    state_info->setId(id);
    state_info->setParentId(state_info_->id());
    state_info->setHost(host);
    state_info->setStateName(description);
    state_info->setDescription(description);
    state_info->setPort(port);
    state_info->setIsRtu(true);
    state_infos_vec_.append(state_info);
    return state_infos_vec_.size();
}

StateInfo* StateGroup::stateInfo() const
{
    return state_info_;
}

const QVector<StateInfo*>& StateGroup::stateInfosVec() const
{
    return state_infos_vec_;
}
